<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket 测试页面</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .status {
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
            font-weight: bold;
        }
        .connected {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .disconnected {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .controls {
            margin-bottom: 20px;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        input[type="text"] {
            width: 70%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin-right: 10px;
        }
        .messages {
            height: 300px;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
            font-family: monospace;
        }
        .message {
            margin-bottom: 10px;
            padding: 5px;
            border-radius: 3px;
        }
        .sent {
            background-color: #e3f2fd;
            border-left: 3px solid #2196f3;
        }
        .received {
            background-color: #f3e5f5;
            border-left: 3px solid #9c27b0;
        }
        .system {
            background-color: #fff3e0;
            border-left: 3px solid #ff9800;
        }
        .timestamp {
            font-size: 0.8em;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🌐 WebSocket 测试页面</h1>
        
        <div id="status" class="status disconnected">
            ❌ 未连接
        </div>
        
        <div class="controls">
            <button id="connectBtn" onclick="connect()">连接</button>
            <button id="disconnectBtn" onclick="disconnect()" disabled>断开连接</button>
            <button onclick="sendPing()">发送 Ping</button>
            <button onclick="clearMessages()">清空消息</button>
        </div>
        
        <div class="controls">
            <input type="text" id="messageInput" placeholder="输入消息..." onkeypress="handleKeyPress(event)">
            <button onclick="sendMessage()">发送消息</button>
        </div>
        
        <div class="controls">
            <button onclick="sendNotification()">发送通知</button>
            <button onclick="sendSystemMessage()">发送系统消息</button>
            <button onclick="getClientCount()">获取客户端数量</button>
        </div>
        
        <div id="messages" class="messages"></div>
    </div>

    <script>
        let ws = null;
        let messageId = 1;

        function connect() {
            if (ws && ws.readyState === WebSocket.OPEN) {
                addMessage('system', '已经连接到服务器');
                return;
            }

            ws = new WebSocket('ws://localhost:8080/ws');
            
            ws.onopen = function(event) {
                updateStatus(true);
                addMessage('system', '✅ 连接成功');
                document.getElementById('connectBtn').disabled = true;
                document.getElementById('disconnectBtn').disabled = false;
            };
            
            ws.onmessage = function(event) {
                try {
                    const data = JSON.parse(event.data);
                    addMessage('received', `收到消息: ${JSON.stringify(data, null, 2)}`);
                } catch (e) {
                    addMessage('received', `收到文本: ${event.data}`);
                }
            };
            
            ws.onclose = function(event) {
                updateStatus(false);
                addMessage('system', `❌ 连接关闭 (代码: ${event.code})`);
                document.getElementById('connectBtn').disabled = false;
                document.getElementById('disconnectBtn').disabled = true;
            };
            
            ws.onerror = function(error) {
                addMessage('system', `❌ 连接错误: ${error}`);
            };
        }

        function disconnect() {
            if (ws) {
                ws.close();
            }
        }

        function sendMessage() {
            const input = document.getElementById('messageInput');
            const message = input.value.trim();
            
            if (!message) {
                alert('请输入消息');
                return;
            }
            
            if (!ws || ws.readyState !== WebSocket.OPEN) {
                alert('请先连接到服务器');
                return;
            }
            
            const messageObj = {
                id: messageId++,
                type: 'user_message',
                content: message,
                timestamp: new Date().toISOString()
            };
            
            ws.send(JSON.stringify(messageObj));
            addMessage('sent', `发送: ${JSON.stringify(messageObj, null, 2)}`);
            input.value = '';
        }

        function sendPing() {
            if (!ws || ws.readyState !== WebSocket.OPEN) {
                alert('请先连接到服务器');
                return;
            }
            
            const pingMsg = {
                type: 'ping',
                timestamp: new Date().toISOString()
            };
            
            ws.send(JSON.stringify(pingMsg));
            addMessage('sent', `发送 Ping: ${JSON.stringify(pingMsg, null, 2)}`);
        }

        function sendNotification() {
            if (!ws || ws.readyState !== WebSocket.OPEN) {
                alert('请先连接到服务器');
                return;
            }
            
            const notification = {
                type: 'notification',
                title: '测试通知',
                message: '这是一个测试通知消息',
                data: { test: true },
                timestamp: new Date().toISOString()
            };
            
            ws.send(JSON.stringify(notification));
            addMessage('sent', `发送通知: ${JSON.stringify(notification, null, 2)}`);
        }

        function sendSystemMessage() {
            if (!ws || ws.readyState !== WebSocket.OPEN) {
                alert('请先连接到服务器');
                return;
            }
            
            const systemMsg = {
                type: 'system_message',
                message: '这是一个系统消息',
                timestamp: new Date().toISOString()
            };
            
            ws.send(JSON.stringify(systemMsg));
            addMessage('sent', `发送系统消息: ${JSON.stringify(systemMsg, null, 2)}`);
        }

        function getClientCount() {
            if (!ws || ws.readyState !== WebSocket.OPEN) {
                alert('请先连接到服务器');
                return;
            }
            
            const countMsg = {
                type: 'get_client_count',
                timestamp: new Date().toISOString()
            };
            
            ws.send(JSON.stringify(countMsg));
            addMessage('sent', `请求客户端数量: ${JSON.stringify(countMsg, null, 2)}`);
        }

        function updateStatus(connected) {
            const status = document.getElementById('status');
            if (connected) {
                status.className = 'status connected';
                status.textContent = '✅ 已连接';
            } else {
                status.className = 'status disconnected';
                status.textContent = '❌ 未连接';
            }
        }

        function addMessage(type, content) {
            const messages = document.getElementById('messages');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${type}`;
            
            const timestamp = new Date().toLocaleTimeString();
            messageDiv.innerHTML = `
                <div class="timestamp">[${timestamp}]</div>
                <div>${content}</div>
            `;
            
            messages.appendChild(messageDiv);
            messages.scrollTop = messages.scrollHeight;
        }

        function clearMessages() {
            document.getElementById('messages').innerHTML = '';
        }

        function handleKeyPress(event) {
            if (event.key === 'Enter') {
                sendMessage();
            }
        }

        // 页面加载时自动连接
        window.onload = function() {
            addMessage('system', '页面已加载，点击"连接"按钮开始测试');
        };
    </script>
</body>
</html>